#include "bsp_config.h"

/********************************************IPC相关的都是全局变量***********************************************************************/
SemaphoreHandle_t time_mutex = NULL;        //互斥量
SemaphoreHandle_t tm1638_mutex = NULL;        //互斥量
xQueueHandle beep_QueueHandler = NULL; //控制蜂鸣器动作的队列
xQueueHandle mqtt_pub_QueueHandler = NULL;             //控制MQTT发布动作的队列句柄
SemaphoreHandle_t mqtt_rev_sem=NULL;                   //mqtt接受线程的二值信号量句柄


static TaskHandle_t MQTTReceiveTaskHandler=NULL;        //MQTT接收线程控制块指针  MQTT接受采用信号量
static TaskHandle_t MQTTPublishTaskHandler=NULL;        //MQTT发布线程控制块指针  MQTT发布采用消息队列
static TaskHandle_t DisplayTaskHandler = NULL;
static TaskHandle_t KeyTaskHandler = NULL;
static TaskHandle_t BeepTaskHandler = NULL;
static TaskHandle_t PIDTaskHandler = NULL;
static TaskHandle_t ControlTaskHandler = NULL;

const char *TAG = "ESP32_MOSQUITO_REPELLENT";

/**
 *  @brief 主函数入口
 *  @param 无
 *  @brief 无  
 */
void app_main(void)
{
    ESP_LOGI(TAG, "[APP] Startup..");
    ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
    ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());

    bsp_config();
    if (time_mutex == NULL)
    {
        time_mutex = xSemaphoreCreateMutex();
    }
    if (tm1638_mutex == NULL)
    {
        tm1638_mutex = xSemaphoreCreateMutex();
    }
    if (beep_QueueHandler == NULL)
    {
        beep_QueueHandler = xQueueCreate(5, sizeof(BEEP_QUEUE_ENUM_Typedef *));
    }
    if (mqtt_pub_QueueHandler == NULL)
    {   //
        mqtt_pub_QueueHandler = xQueueCreate(5,  sizeof(MQTT_PUB_QUEUE_ENUM_Typedef *));  //
    }
    if (mqtt_rev_sem == NULL)
    {   //创建MQTT接受线程的二值信号量
        mqtt_rev_sem = xSemaphoreCreateBinary(); 
    }

    /**************************************************************************************************/

    if (MQTTReceiveTaskHandler == NULL)
    {   //MQTT消息接受线程     主要就是将MQTT接收到的JSON格式信息解析出来,然后根据相应信息执行相应的动作
        xTaskCreate(MQTT_REV_Driver_Task, "mqtt_rev_task", 1024 * 8, NULL, 16, &MQTTReceiveTaskHandler);
    }
    if (MQTTPublishTaskHandler == NULL)
    {   //MQTT消息发布线程     主要就是将要发送的信息打包成JSON格式,然后发送出去   
        xTaskCreate(MQTT_PUB_Driver_Task, "mqtt_pub_task", 1024 * 8, NULL, 15, &MQTTPublishTaskHandler);
    }
    if (PIDTaskHandler == NULL)
    { //按键扫描线程
        xTaskCreate(Heat_Pid_Driver_Task, "pid_task", 1024 * 4, NULL, 14, &PIDTaskHandler);
    }
    if (ControlTaskHandler == NULL)
    { //控制线程
        xTaskCreate(Control_Driver_Task, "control_task", 1024 * 6, NULL, 13, &ControlTaskHandler);
    }
    if (KeyTaskHandler == NULL)
    { //按键扫描线程
        xTaskCreate(Key_Driver_Task, "key_task", 1024 * 4, NULL, 12, &KeyTaskHandler);
    }
    if (BeepTaskHandler == NULL)
    { //蜂鸣器动作线程
        xTaskCreate(Beep_Driver_Task, "beep_task", 1024 * 4, NULL,11, &BeepTaskHandler);
    }
    if (DisplayTaskHandler == NULL)
    { //按键扫描线程
        xTaskCreate(Display_Driver_Task, "display_task", 1024 * 4, NULL,10, &DisplayTaskHandler);
    }
}
